﻿module Lhz.Page {
    export class Login extends Page {
        private sci = new Lhz.ServiceCenter.Invoker();
        private sc = new Lhz.Page.StaticData();

        constructor() {
            super({ checkLogin: false, checkRole: false });
            if (this.indexInitialized) {
                this.bindSelect("#selectSkin", this.sc.getSkins());
                var skin = this.getLocalStorage("skin");
                if (skin) {
                    $("#selectSkin").val(skin);
                }
                $("#selectSkin").change(this, function (event) {
                    var login: Login = event.data;
                    var skin = $(event.target).val();
                    login.setLocalStorage("skin", skin);
                });
                var buttonLogin = $("#buttonLogin");
                buttonLogin.button({ icons: { primary: "ui-icon-key" } }).click(this, function (event) {
                    buttonLogin.button("disable");
                    var login: Login = event.data;
                    var username = $("#textUsername").val();
                    var password = $("#textPassword").val();
                    login.sci.InvokeByInvokeArgument<Lhz.Entity.ServiceCenter.User>({
                        serviceNo: "user",
                        operationNo: "queryByUsernameAndPassword",
                        arguments: [username, password],
                        successCallback: function (result, data) {
                            buttonLogin.button("enable");
                            var login: Login = data;
                            if (result.Result == null) {
                                alert("用户名或密码错误。");
                                $(event.target).button("enable");
                            }
                            else {
                                var user = result.Result;
                                login.setSessionStorage("user", JSON.stringify(user));
                                window.location.href = "index.html";
                            }
                        },
                        data: login
                    });
                });
            }
        }
    }
    $(function () {
        new Login();
    });
}
